package com

import java.util.Date;

class GastosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [gastosInstanceList: Gastos.list(params), gastosInstanceTotal: Gastos.count()]
    }

    def create = {
        def gastosInstance = new Gastos()
        gastosInstance.properties = params
        return [gastosInstance: gastosInstance]
    }

    def save = {
        def gastosInstance = new Gastos(params)
        if (gastosInstance.save(flush: true)) {
			println("SAVE -> params:  "+ params)
			int i = 1
			
			//criando contas a receber
			Date date = new Date()
			
			int parcela = Integer.parseInt(params.parcelas)
				
			double valorParcela = gastosInstance.valor /gastosInstance.parcelas
			
			if (! gastosInstance.entrada){
				println ("sem entrada somar mais 30"+ date)
				date = date + 30
			}
			
			while (i <= parcela) {
				geraContaReceber(gastosInstance, date, valorParcela)
				 i = i +1
				date = date + 30
			}
			
			
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'gastos.label', default: 'Gastos'), gastosInstance.id])}"
            redirect(action: "show", id: gastosInstance.id)
        }
        else {
            render(view: "create", model: [gastosInstance: gastosInstance])
        }
    }

    def show = {
        def gastosInstance = Gastos.get(params.id)
        if (!gastosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gastos.label', default: 'Gastos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [gastosInstance: gastosInstance]
        }
    }

    def edit = {
        def gastosInstance = Gastos.get(params.id)
        if (!gastosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gastos.label', default: 'Gastos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [gastosInstance: gastosInstance]
        }
    }

    def update = {
        def gastosInstance = Gastos.get(params.id)
        if (gastosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (gastosInstance.version > version) {
                    
                    gastosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'gastos.label', default: 'Gastos')] as Object[], "Another user has updated this Gastos while you were editing")
                    render(view: "edit", model: [gastosInstance: gastosInstance])
                    return
                }
            }
            gastosInstance.properties = params
            if (!gastosInstance.hasErrors() && gastosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'gastos.label', default: 'Gastos'), gastosInstance.id])}"
                redirect(action: "show", id: gastosInstance.id)
            }
            else {
                render(view: "edit", model: [gastosInstance: gastosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gastos.label', default: 'Gastos'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def gastosInstance = Gastos.get(params.id)
        if (gastosInstance) {
            try {
                gastosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'gastos.label', default: 'Gastos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'gastos.label', default: 'Gastos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gastos.label', default: 'Gastos'), params.id])}"
            redirect(action: "list")
        }
    }
	/**
	 * 
	 * @param gasto
	 * @param date
	 * @param valorParcela
	 */
	public void geraContaReceber(Gastos gasto, Date date, double valorParcela){
		println ("gerando contas a receber")
		println ("Date"+ date)
		ContasPagar contasPagar = new ContasPagar()
		contasPagar.valor = valorParcela
		contasPagar.gasto = gasto
		contasPagar.dataVencimento = date
		//contasReceber.dataBaixa = date + 30
		contasPagar.save()
	}
}
